<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Unit tests for the loading TinyMCE asynchronously</title>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<link rel="stylesheet" href="http://code.jquery.com/qunit/qunit-git.css" type="text/css" />
<script src="http://code.jquery.com/qunit/qunit-git.js"></script>
<script src="qunit/connector.js"></script>
<script type="text/javascript" src="qunit/runner.js"></script>
<script>
QUnit.config.reorder = false;
QUnit.config.autostart = false;


// This test can only be run with a built version of tinyMCE since the dev
// version uses document.write to put tiny on the page :(
module("Loading TinyMCE asynchronously", {
	autostart: false
});

// kicks off the test by installing an interval checking for domready.
// Not a robust or typical domready check, but suitable for the test.
var attempts = 0;
var readyStateTimer = setInterval(function() {
	if (document.readyState !== 'complete' && attempts < 100) return;
	clearInterval(readyStateTimer);
	loadTinyMCE();
}, 20);

var loadTinyMCE = function() {
	injectScript('../jscripts/tiny_mce/tiny_mce_src.js', initEditor);
};

// simple script injection helper
var injectScript = (function() {
	var relative = document.getElementsByTagName('script')[0];
	var loadEvent = 'onload' in relative ? 'onload' : 'onreadystatechange';
	return function(src, callback) {
		var script = document.createElement('script');
		script.async = 1;
		script.src = src;
		if (callback) script[loadEvent] = callback;
		relative.parentNode.insertBefore(script, relative);
		return script;
	}
}());

var initEditor = function(script) {
	tinyMCE.init({
		mode : 'exact',
		elements : 'elm1',
		init_instance_callback : runTests
	});
};

var runTests = function(editor) {
	test('editor initialized with async load of tinyMCE', 1, function() {
		editor.focus();
		editor.setContent('test 123');
		equal(editor.getContent(), '<p>test 123</p>');
	});
	QUnit.start();
};
</script>
</head>
<body>
	<h1 id="qunit-header">Unit tests for the loading TinyMCE asynchronously</h1>
	<h2 id="qunit-banner"></h2>
	<div id="qunit-testrunner-toolbar"></div>
	<h2 id="qunit-userAgent"></h2>
	<ol id="qunit-tests"></ol>
	<textarea id="elm1" name="elm1"></textarea>
</body>
</html>